﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MicroFractal.iTunesBrowser.Business;
using MicroFractal.iTunesBrowser.Core;
using MicroFractal.iTunesBrowser.Core.Device;
using MicroFractal.iTunesBrowser.Controls;

namespace MicroFractal.iTunesBrowser
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            lstDevices.ItemsSource = DeviceManager.Instance.Devices;
            SetControlsVisible(false);
        }

        private void lstDevices_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            myLibrary.BeginUpdate();
            myLibrary.Covers.Clear();
            Cursor = Cursors.Wait;

            foreach (Album a in LibraryManager.GetLibrary((IDevice)lstDevices.SelectedItem, false).Albums)
            {
                //if (a.Cover != null)
                //    a.Cover.Save(string.Format("{0}\\{1}.{2}", "C:", a.Name, "bmp"));

                Cover c = new Cover(a);
                c.CoverImage = a.Cover;
                //Cover c = new Cover(string.Format("{0}\\{1}.{2}", "C:", a.Name, "bmp"));
                c.ArtistName = a.Artist.Name;
                c.AlbumName = a.Name;

                myLibrary.Covers.Add(c);
            }
            myLibrary.EndUpdate();
            Cursor = Cursors.Arrow;
        }

        private void image1_Click(object sender, RoutedEventArgs e)
        {
            using (System.Windows.Forms.FolderBrowserDialog f = new System.Windows.Forms.FolderBrowserDialog())
            {
                if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    DeviceManager.Instance.CreateFolderDevice(f.SelectedPath);
                }
            }

        }

        private void cmdDelete_Click(object sender, RoutedEventArgs e)
        {
            if (lstDevices.SelectedItem != null && lstDevices.SelectedItem is IDevice)
            {
                ((IDevice)lstDevices.SelectedItem).Dispose();
            }
        }

        private void cmdDownload_Click(object sender, RoutedEventArgs e)
        {
            if (myLibrary.SelectedCover != null)
            {
                using (System.Windows.Forms.FolderBrowserDialog f = new System.Windows.Forms.FolderBrowserDialog())
                {
                    if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        IDevice d = ((IDevice)lstDevices.SelectedItem);
                        d.DownloadCompleted +=new DownloadProgressEventHandler(Window1_DownloadCompleted);
                        d.DownloadProgressChange +=new DownloadProgressEventHandler(d_DownloadProgressChange);
                        List<IDownloadableObject> l = new List<IDownloadableObject>();
                        l.Add(myLibrary.SelectedCover.Album);
                        statusProgress.Value = 0;
                        statusMessage.Text = "Begining Download";
                        SetControlsVisible(true);
                        d.AsyncDownload(l, f.SelectedPath, true);
                    }
                }
                
            }
        }

        void d_DownloadProgressChange(object sender, MicroFractal.iTunesBrowser.Core.Device.DownloadProgressEventArgs e)
        {
            if (e.NextDownloadingSong != null)
                this.statusMessage.Text = string.Format("{0} - {1}", e.NextDownloadingSong.Artist.Name, e.NextDownloadingSong.Title);
            
            this.statusProgress.Value = e.Progress;
        }

        void Window1_DownloadCompleted(object sender, MicroFractal.iTunesBrowser.Core.Device.DownloadProgressEventArgs e)
        {
            IDevice d = (IDevice)sender;
            d.DownloadCompleted -= new DownloadProgressEventHandler(Window1_DownloadCompleted);
            d.DownloadProgressChange -= new DownloadProgressEventHandler(d_DownloadProgressChange);

            this.statusProgress.Value = 0;
            this.statusMessage.Text = "Download Complete";
        }

        private void cmdCancel_Click(object sender, RoutedEventArgs e)
        {
            IDevice d = ((IDevice)lstDevices.SelectedItem);
            if (d.CancelDownload())
            {
                statusMessage.Text = "Download Canceled";
                SetControlsVisible(false);
            }
        }

        private void SetControlsVisible(bool visible)
        {
            Visibility v = visible? Visibility.Visible: Visibility.Hidden;
            cmdCancel.Visibility = v;
            statusMessage.Visibility = v;
            statusProgress.Visibility = v;
        }

    }
}
